<?php

/*
 * 项目重构小工具
 * @version 2017-12-26
 */

// 判断是否重复执行
if (is_file('refactor.lock')) {
    exit('请勿重复执行');
}
file_put_contents('refactor.lock', 1);

// 在这里执行代码
refactor_udml5();

// 输出结果
echo 'success';

// ===============================函数封装==============================

/**
 * 重构UDML5
 * @param type $path
 * @version 2018-1-24
 */
function refactor_udml5($path = './') {
    $file = new FilesystemIterator($path);
    $filename = '';
    $filepath = '';
    $filepathtmp = '';
    $category = '';
    foreach ($file as $fileinfo) {
        $filename = $fileinfo->getFilename();
        $filepath = $path . $filename;
        if ($fileinfo->isFile()) {
            if (false === strpos($filename, '.sql')) {
                continue;
            }
            if (false !== strpos($filename, 't_porg_')) {
                $category = 't_' . str_replace('org_', '', basename($path)) . '_';
                if (strpos($filename, $category)) {
                    $filepathtmp = $path . str_replace('t_porg_', 't_', $filename);
                } else {
                    $filepathtmp = $path . str_replace('t_porg_', $category, $filename);
                }
                rename($filepath, $filepathtmp);
                $filepath = $filepathtmp;
            }
            $content = file_get_contents($filepath);
            $content = str_replace('t_porg_', 't_', $content);
            $content = str_replace('重命名为t_', ' 重命名为 t_', $content);
            $content = str_replace(' 由t_', ' 由 t_', $content);
            if ($category && false === strpos($content, $category)) {
                $content = str_replace('exists t_', 'exists ' . $category, $content);
            }
            $content = str_replace('\r\n', '\r\n' . PHP_EOL, $content);
            $content = str_replace(".';", '.\r\n' . PHP_EOL . "';", $content);
            file_put_contents($filepath, $content);
        } else {
            refactor_udml5($filepath . '/');
        }
    }
}
/**
 * 重构TP控制层由TP3.x到TP5.x
 * @param type $path
 * @version 2017-12-26
 */
function refactor_controller_tp3to5($path = './admin/Action/') {
    $file = new FilesystemIterator($path);
    $filename = '';
    foreach ($file as $fileinfo) {
        $filename = $fileinfo->getFilename();
        if ($fileinfo->isFile()) {
            if (strpos($filename, 'Action.class')) {
                rename($path . $filename, $path . str_replace('Action.class', 'Ctl', $filename));
            }
        }
    }
    foreach ($file as $fileinfo) {
        $filename = $fileinfo->getFilename();
        if ($fileinfo->isFile()) {
            $content = file_get_contents($path . $filename);
            if (false === strpos($content, 'namespace ')) {
                $content = str_replace('<?php', '<?php' . PHP_EOL . PHP_EOL . 'namespace dashboard\admin\controller;' . PHP_EOL, $content);
                file_put_contents($path . $filename, $content);
            }
        }
    }
}

/**
 * 重构Fanwelive的AutoCache目录
 * @param type $path
 * @version 2017-12-26
 */
function refactor_autocache_fwlive($path = './admin/model/cache/') {
    $file = new FilesystemIterator($path);
    $filename = '';
    $url = '';
    $prefix = ''; // 树形层级图形
    $isactive = ''; // 选中效果类
    foreach ($file as $fileinfo) {
        if ($fileinfo->isFile()) {
            $filename = $fileinfo->getFilename();
            if (strpos($filename, '.auto_cache')) {
                rename($path . $filename, $path . str_replace('.auto_cache', '', $filename));
            }
        }
    }
    foreach ($file as $fileinfo) {
        if ($fileinfo->isFile()) {
            $content = file_get_contents($path . $filename);
            if (strpos($content, ' extends auto_cache')) {
                preg_match("/class ([\S]+) extends auto_cache/", $content, $match);
                $className = $match[1];
                $className = str_replace('_auto_cache', '_cache', $className);
                $className = str_replace('_', '', ucwords($className, '_'));
                $content = preg_replace("/class ([\S]+) extends auto_cache/", 'class ' . $className . ' ', $content);
                $content = str_replace('public function ', 'public static function ', $content);
                $content = str_replace('class ', 'namespace dashboard\admin\model\cache;' . PHP_EOL . PHP_EOL . 'class ', $content);
                file_put_contents($path . $filename, $content);
            }
            $filename = $fileinfo->getFilename();
            $basename = $fileinfo->getBasename('.php');
            if (strpos($filename, '_')) {
                rename($path . $filename, $path . str_replace('_', '', ucwords($basename, '_')) . 'Cache.php');
            }
        }
    }
}